package fr.kotton.myorganizer.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;

import fr.kotton.myorganizer.bean.Task;

public class EndDateComparator implements Comparator<Object> {

    @Override
    public int compare(Object o1, Object o2) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("EE dd MMM yyyy HH:mm");

        Date lDate1 = new Date();
        Date lDate2 = new Date();

        if (o1 instanceof Task) {
            lDate1 = ((Task) o1).getEndDate();
        } else if (o1 instanceof String) {
            try {
                lDate1 = dateFormat.parse(((String) o1) + " 00:00");
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        if (o2 instanceof Task) {
            lDate2 = ((Task) o2).getEndDate();
        } else if (o2 instanceof String) {
            try {
                lDate2 = dateFormat.parse(((String) o2) + " 00:00");
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        if (lDate1.compareTo(lDate2) > 0) {
            return 1;
        } else if (lDate1.compareTo(lDate2) < 0) {
            return -1;
        } else if (lDate1.compareTo(lDate2) == 0) {
            if (o1 instanceof Date && o2 instanceof Date) {
                return 0;
            } else if (o1 instanceof Date && o2 instanceof String) {
                return -1;
            } else if (o1 instanceof String && o2 instanceof Date) {
                return 1;
            } else if (o1 instanceof String && o2 instanceof String) {
                return 0;
            }
        }

        // Not accessible
        return 0;
    }
}